package com.yq.testmodule.paging3

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.yq.testmodule.R
import com.yq.testmodule.entity.NewsListEntity

class TestPagingAdapter(private val context: Context): PagingDataAdapter<NewsListEntity, TestPagingAdapter.ViewHolder>(COMPARATOR) {
    
    companion object{
        private val COMPARATOR = object : DiffUtil.ItemCallback<NewsListEntity>() {
            override fun areItemsTheSame(oldItem: NewsListEntity, newItem: NewsListEntity): Boolean {
                return oldItem.id == newItem.id
            }

            override fun areContentsTheSame(oldItem: NewsListEntity, newItem: NewsListEntity): Boolean {
                return oldItem == newItem
            }
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = getItem(position)
        item?.let {
            holder.tvName.text = it.title
        }


    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_test_paging3, parent, false)
        return ViewHolder(view)
    }


    class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
        val tvName: TextView = itemView.findViewById(R.id.tv_name)
    }
}
